#include <iostream>
#include <vector>
#include <queue>

int main()
{
    int N, L;
    std::cin >> N;
    std::cin >> L;

    std::vector<std::pair<int, int>> nodes[N + 1];
    std::vector<bool> vis(N + 1, 0);
    std::vector<int> dis(N + 1, 1e9);

    for (int i = 0; i < L; i++)
    {
        int u, v, w;
        std::cin >> u >> v >> w;
        nodes[u].push_back({w, v});
    }

    int s;
    std::cin >> s;
    std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<std::pair<int, int>>> q;
    q.push({0, s});
    dis[s] = 0;
    while (!q.empty())
    {
        int u = q.top().second;
        q.pop();
        if (vis[u])
            continue;
        vis[u] = 1;

        for (int i = 0; i < nodes[u].size(); i++)
        {
            auto p = nodes[u][i];
            int w = p.first;
            int v = p.second;
            if (dis[u] + w < dis[v])
            {
                dis[v] = dis[u] + w;
                q.push({dis[v], v});
            }
        }
    }

    int ans = 0;
    int cnt = 0;
    for (int i = 1; i <= N; i++)
    {
        ans = std::max(ans, dis[i]);
        cnt += vis[i];
    }

    if (cnt < N)
        std::cout << -1 << std::endl;
    else
        std::cout << ans << std::endl;

    return 0;
}